package itbour.onetouchshow.widget.inching;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import java.util.List;

import itbour.onetouchshow.bean.canvas.CompoundPathsBean;
import itbour.onetouchshow.bean.canvas.PathsBean;
import itbour.onetouchshow.bean.inching.noppt.InchingOutLineImgBean;
import itbour.onetouchshow.utils.CommonUtils;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by zjl_d on 2017/12/6.
 * 图片专用的微调
 */

public class InchingOpreaImageView extends View {
    //用于绘制动态蒙板
    public boolean onMaskChange = false;
    //这里在onMaskChange 的时候要计算传入 并非原始设置的宽高
    public float ivW = -1.0f;
    public float ivH = -1.0f;
    public Path path;
    private Bitmap bitmap;
    private InchingOutLineImgBean data;
    //当手指在控制点拖动的时候不需要裁切 否则需要裁剪
    private boolean isNeedClip = true;
    private Paint maskPaint;

    //当前显示
    public InchingOpreaImageView(Context context) {
        this(context, null);
    }

    public InchingOpreaImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public InchingOpreaImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initPaint();
    }

    public void setNeedClip(boolean needClip) {
        isNeedClip = needClip;
    }

    public void setImgBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    private void initPaint() {
        maskPaint = new Paint();
        maskPaint.setColor(Color.BLACK); //设置画笔颜色
        maskPaint.setStrokeWidth(5);//设置画笔宽度
        maskPaint.setStyle(Paint.Style.FILL);

        maskPaint.setAntiAlias(true);
    }

    /**
     * 用于绘制动态蒙板
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (bitmap == null) {
            return;
        }
        if (data == null) {
            return;
        }
        CompoundPathsBean compoundPathsBean = data.getMask().get(0);
        List<PathsBean> paths = compoundPathsBean.getPaths();
        int width = canvas.getWidth();
        int height = canvas.getHeight();

        int w = (int) compoundPathsBean.getW();
        int h = (int) compoundPathsBean.getH();
        L_.i("图片测试===imageViewW===" + width + "imageViewH===" + height + "getX()===" + getX() + "getY===" + getY());
        //除了本控件在控制点移动时 其余情况都需要裁切
        if (!isNeedClip) {


            final RectF rectF = new RectF(UIUtils.dp2px(1), UIUtils.dp2px(1), width - UIUtils.dp2px(1), height - UIUtils.dp2px(1));
            canvas.drawBitmap(bitmap, null, rectF, maskPaint);
            //save as new layer
            //默认 后绘制的在上层
            int save = canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);

            maskPaint.setColor(Color.parseColor("#90bcbcbc"));
            canvas.drawRect(0, 0, width, height, maskPaint);

//            Path path = CommonUtils.getTextOrShapeBesselPaths(paths, width, height, w, h);
            Path path = new Path();

            maskPaint.setColor(Color.parseColor("#000000"));
            //后绘制的在上层 取下层绘制非交集部分
            maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
            //v60在拖动时 蒙版和图片变化
            if (path != null && onMaskChange) {
                path = this.path;
            }
            canvas.drawPath(path, maskPaint);
            maskPaint.setXfermode(null);
            //restore to canvas
            canvas.restoreToCount(save);
        } else {
            Path path = CommonUtils.getTextOrShapeBesselPaths(paths, width, height, w, h);
            canvas.clipPath(path);
            if (bitmap != null) {
                RectF rectF = new RectF(0, 0, width, height);
                canvas.drawBitmap(bitmap, null, rectF, maskPaint);
            }
        }

    }

    public void setData(InchingOutLineImgBean data) {
        this.data = data;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (data == null) {
            return;
        }
        setMeasuredDimension(measuredWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    public int measuredWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = (int) data.getImage().getW();
            if (onMaskChange) {
                if (Math.abs(ivW - (-1.0f)) > 0.000001) {
                    result = (int) ivW;
                }

            }
        }
        L_.i("measuredWidth===" + result);
        return result;
    }

    public int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = (int) data.getImage().getH();
            if (onMaskChange) {
                if (Math.abs(ivH - (-1.0f)) > 0.000001) {
                    result = (int) ivH;
                }

            }
        }
        L_.i("measureHeight===" + result);

        return result;
    }

    public void refreshInchingImageViewMask(float x, float y) {


        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) this.getLayoutParams();
        layoutParams.leftMargin = (int) x;
        layoutParams.topMargin = (int) y;
        this.setLayoutParams(layoutParams);


    }

    public void setOnMaskChange(boolean onMaskChange, Path path, float ivW, float ivH) {
        this.path = path;
        this.onMaskChange = onMaskChange;
        this.ivW = ivW;
        this.ivH = ivH;
    }

    public void refreshView(int w, int h) {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) (this).getLayoutParams();
        layoutParams.width = w;
        layoutParams.height = h;
        this.setLayoutParams(layoutParams);
        this.postInvalidate();
    }
}
